package frame

import (
	"gitee.com/git_public/vfaith/builtin/bean"
	"gitee.com/git_public/vfaith/builtin/vault"
	"gitee.com/git_public/vfaith/fount/persist"
	"gitee.com/git_public/vfaith/sugar"
	"gitee.com/git_public/vfaith/sugar/reflectx"
	"github.com/jmoiron/sqlx"
)

// region 结构体定义&构造

type EntityBiz[D IxBaseDaoPO[PO], PO bean.IxBaseEntityPO] struct {
	*sqlx.DB
	Dao D
}

func NewEntityBiz[D IxBaseDaoPO[PO], PO bean.IxBaseEntityPO]() *EntityBiz[D, PO] {
	//直接使用new(B)实例化出来的是IxEntityBiz
	entityBiz := reflectx.NewInstance[*EntityBiz[D, PO]]()
	entityBiz.DB = persist.GetOexDefault().DB
	entityBiz.Dao = GetDao[D](sugar.GetPkgParent(2))

	return entityBiz
}

// endregion

// region 标准CRUD

func (entityBiz *EntityBiz[D, PO]) TotalRecord(args ...*vault.QueryParam) (int, error) {
	param := vault.GetDefaultQueryParam(args)
	return entityBiz.Dao.TotalRecord(param)
}

func (entityBiz *EntityBiz[D, PO]) FindList(args ...*vault.QueryParam) ([]PO, error) {
	param := vault.GetDefaultQueryParam(args)
	return entityBiz.Dao.FindList(param)
}

func (entityBiz *EntityBiz[D, PO]) FindNumList(args ...*vault.QueryParam) (int, []PO, error) {
	param := vault.GetDefaultQueryParam(args)
	return entityBiz.Dao.FindNumList(param)
}

func (entityBiz *EntityBiz[D, PO]) FindTable(args ...*vault.QueryParam) (*vault.TableMap, error) {
	param := vault.GetDefaultQueryParam(args)
	return entityBiz.Dao.FindTable(param)
}

func (entityBiz *EntityBiz[D, PO]) FindModel(id int) (PO, error) {
	return entityBiz.Dao.FindModel(id)
}

func (entityBiz *EntityBiz[D, PO]) Create(data PO) (int64, error) {
	return entityBiz.Dao.Create(data)
}

func (entityBiz *EntityBiz[D, PO]) Update(data PO) (int64, error) {
	return entityBiz.Dao.Update(data)
}

func (entityBiz *EntityBiz[D, PO]) Delete(id int) (int64, error) {
	return entityBiz.Dao.Delete(id)
}

// endregion

//region 事务方法

func (entityBiz *EntityBiz[D, PO]) TotalRecordByTx(param *vault.QueryParam, tx *sqlx.Tx) (int, error) {
	return entityBiz.Dao.TotalRecord(param, tx)
}

func (entityBiz *EntityBiz[D, PO]) FindListByTx(param *vault.QueryParam, tx *sqlx.Tx) ([]PO, error) {
	return entityBiz.Dao.FindList(param, tx)
}

func (entityBiz *EntityBiz[D, PO]) FindNumListByTx(param *vault.QueryParam, tx *sqlx.Tx) (int, []PO, error) {
	return entityBiz.Dao.FindNumList(param, tx)
}

func (entityBiz *EntityBiz[D, PO]) FindModelByTx(id int, tx *sqlx.Tx) (PO, error) {
	return entityBiz.Dao.FindModel(id, tx)
}

func (entityBiz *EntityBiz[D, PO]) CreateByTx(data PO, tx *sqlx.Tx) (int64, error) {
	return entityBiz.Dao.Create(data, tx)
}

func (entityBiz *EntityBiz[D, PO]) UpdateByTx(data PO, tx *sqlx.Tx) (int64, error) {
	return entityBiz.Dao.Update(data, tx)
}

func (entityBiz *EntityBiz[D, PO]) DeleteByTx(id int, tx *sqlx.Tx) (int64, error) {
	return entityBiz.Dao.Delete(id, tx)
}

//endregion

// region 辅助方法

// endregion
